package com.ruoyi.web.controller.system;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.SysDriverOrder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.api.service.ISysDriverOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【订单】Controller
 *
 * @author zyy
 * @date 2024-02-06
 */
@RestController
@RequestMapping("/api/order")
public class SysDriverOrderController extends BaseController {
    @Autowired
    private ISysDriverOrderService sysDriverOrderService;

    /**
     * 查询【订单】列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(SysDriverOrder sysDriverOrder) {
        startPage();
        List<SysDriverOrder> list = sysDriverOrderService.selectSysDriverOrderList(sysDriverOrder);
        return getDataTable(list);
    }

    /**
     * 导出【订单】列表
     */
    @Anonymous
    @Log(title = "【导出订单】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDriverOrder sysDriverOrder) {
        List<SysDriverOrder> list = sysDriverOrderService.selectSysDriverOrderList(sysDriverOrder);
        ExcelUtil<SysDriverOrder> util = new ExcelUtil<SysDriverOrder>(SysDriverOrder.class);
        util.exportExcel(response, list, "【payment】数据");
    }

    /**
     * 获取【订单】详细信息
     */
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sysDriverOrderService.selectSysDriverOrderById(id));
    }

    /**
     * 新增【订单】
     */
    @Anonymous
    @Log(title = "【新增订单】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDriverOrder sysDriverOrder) {
        //去除连字符
        String id = UUID.fastUUID().toString().replaceAll("-", "");
        sysDriverOrder.setId(id);
        //设置当前时间
        if (sysDriverOrder.getTime() == null) {
            Date date = new Date(System.currentTimeMillis());
            sysDriverOrder.setTime(date);
        }
        sysDriverOrder.setStatus(0L);
        //保留两位小数
        DecimalFormat df = new DecimalFormat("0.00");
        sysDriverOrder.setPay(Double.valueOf(df.format(sysDriverOrder.getPay())));
        sysDriverOrder.setDistance(Double.valueOf(df.format(sysDriverOrder.getDistance())));
        int res = sysDriverOrderService.insertSysDriverOrder(sysDriverOrder);
        if (res == 0) {
            return error();
        }
        return success(sysDriverOrder);
    }

    /**
     * 修改【订单】
     */
    @Anonymous
    @Log(title = "【修改订单】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDriverOrder sysDriverOrder) {
        return toAjax(sysDriverOrderService.updateSysDriverOrder(sysDriverOrder));
    }

    /**
     * 删除【订单】
     */
    @Anonymous
    @Log(title = "【删除订单】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysDriverOrderService.deleteSysDriverOrderByIds(ids));
    }
}
